##########################################################################################

library(data.table)
library(optparse)
library(parallel)

##########################################################################################

option_list <- list(
    make_option(c("--sample_list_file"), type = "character"),
    make_option(c("--sample_list_public_file"), type = "character"),
    make_option(c("--tmm_file"), type = "character"),
    make_option(c("--tmm_combinepublic_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    sample_list_file <- "~/20220915_gastric_multiple/dna_combinePublic/config/tumor_normal.class.list"
    sample_list_public_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/combine/MutationInfo.combine.tsv"
    tmm_file <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA/CombineTPM.FilterLowExpression.tsv"
    tmm_combinepublic_file <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA/TCGA.FilterLowExpression.TPM.tsv"
    out_path <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA"

}

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

sample_list_file <- opt$sample_list_file
sample_list_public_file <- opt$sample_list_public_file
tmm_file <- opt$tmm_file
tmm_combinepublic_file <- opt$tmm_combinepublic_file
out_path <- opt$out_path

##########################################################################################

info <- data.frame(fread(sample_list_file))
info_public <- data.frame(fread(sample_list_public_file))
info_public <- subset( info_public , MS_Type=="MSS" )
info_public$ID <- paste0( info_public$Tumor , "_" , info_public$Class )

dat_tpm <- data.frame(fread(tmm_file))
colnames(dat_tpm) <- gsub("[.]" , "-" , colnames(dat_tpm))
dat_tpm_public_combine <- data.frame(fread(tmm_combinepublic_file))
colnames(dat_tpm_public_combine) <- gsub("[.]" , "-" , colnames(dat_tpm_public_combine))

##########################################################################################

dat_tpm <- dat_tpm[,-1]
colnames(dat_tpm_public_combine)[1] <- "Hugo_Symbol"

tcga_sample <- colnames(dat_tpm_public_combine)[colnames(dat_tpm_public_combine) %in% info_public$ID]
dat_tpm_public_combine <- dat_tpm_public_combine[,c("Hugo_Symbol" , tcga_sample)]

##########################################################################################

sid <- paste0(info$ID , "_" , info$Class_sub )
#sid_normal <- paste0(info$ID , "_Normal" )
result <- dat_tpm[,colnames(dat_tpm) %in% c("Hugo_Symbol" , sid)]
## 去除TCGA的Normal

##########################################################################################

dat_tpm_combine <- merge( result , dat_tpm_public_combine , by = "Hugo_Symbol" )
dat_tpm_combine$gene_id <- dat_tpm_combine$Hugo_Symbol
dat_tpm_combine <- dat_tpm_combine[,-1]
#dat_tpm_combine <- dat_tpm_combine[ , grep( "Normal" , colnames(dat_tpm_combine) , invert = T )]

out_file <- paste0( out_path , "/" , "CombineTPM.DNAUse.NJMU_TCGA.tsv" )
write.table( dat_tpm_combine , out_file , row.names = F , sep = "\t" , quote = F )
